package com.example.test2.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.test2.R;
import com.example.test2.databinding.ActivityAddClothesBinding;
import com.example.test2.db.control.ClothesDbController;
import com.example.test2.db.entity.Clothes;
import com.example.test2.http.Constants;
import com.example.test2.utils.FileUtils;
import com.example.test2.utils.ToastUtils;
import com.example.test2.utils.Tools;

public class AddClothesActivity extends BaseActivity<ActivityAddClothesBinding>implements View.OnClickListener {
    @Override
    protected void initData() {
        super.initData();
        getBinding().titleBar.setTitle("发布商品");
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().btSend.setOnClickListener(this);
        getBinding().ivImg.setOnClickListener(this);
        getBinding().titleBar.setLeftIconOnClickListener(view -> {finish();});
    }
    private Uri uri;

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1000 && resultCode == Activity.RESULT_OK && null != data) {
            uri = data.getData();
            getBinding().ivImg.setImageURI(uri);
        }
    }
    private void selectImage() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("image/*");
        startActivityForResult(intent, 1000);
    }
    @Override
    protected int initLayout() {
        return R.layout.activity_add_clothes;
    }

    @Override
    protected ActivityAddClothesBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityAddClothesBinding.inflate(layoutInflater);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_send:
                send();
                break;
            case R.id.iv_img:
                selectImage();
                break;
        }
    }
    private void send() {
        String name = getBinding().etName.getText().toString();
        String price = getBinding().etPrice.getText().toString();
        String address = getBinding().etAddress.getText().toString();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(price) || TextUtils.isEmpty(address)) {
            ToastUtils.getInstance().show(AddClothesActivity.this, "标题、单价、发货地址不能为空");
            return;
        }
        String type = getBinding().etType.getText().toString();
        String cost = getBinding().etCost.getText().toString();
        String safeguard = getBinding().etSafeguard.getText().toString();
        String comment = getBinding().etComment.getText().toString();

        String path = FileUtils.getPath(this, uri, name);

        Clothes clothes = new Clothes();
        clothes.setName(name);
        clothes.setType(type);
        clothes.setPrice(price);
        clothes.setPath(path);
        clothes.setMsg(comment);
        clothes.setPhone(Constants.PHONE);
        clothes.setView(Tools.getNum(100, 200) + "");
        clothes.setAddress(address);
        clothes.setSafeguard(safeguard);
        clothes.setShopName(Constants.PHONE);
        clothes.setCost(cost);
        clothes.setUserAdd(true);

        ClothesDbController.getInstance(AddClothesActivity.this).insertOrReplace(clothes);
        ToastUtils.getInstance().show(AddClothesActivity.this, "发布成功");
        new Handler().postDelayed(() -> {
            finish();
        }, 500);
    }
}
